package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.Constant;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.ClientLogBo;
import com.ruoyi.tb.lmt.domain.vo.ClientLogVo;
import com.ruoyi.tb.lmt.domain.ClientLog;
import com.ruoyi.tb.lmt.mapper.ClientLogMapper;
import com.ruoyi.tb.lmt.service.IClientLogService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户端日志Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@RequiredArgsConstructor
@Service
public class ClientLogServiceImpl implements IClientLogService {

    private final ClientLogMapper baseMapper;

    /**
     * 查询客户端日志
     */
    @Override
    public ClientLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户端日志列表
     */
    @Override
    public TableDataInfo<ClientLogVo> queryPageList(ClientLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClientLog> lqw = buildQueryWrapper(bo);
        Page<ClientLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户端日志列表
     */
    @Override
    public List<ClientLogVo> queryList(ClientLogBo bo) {
        LambdaQueryWrapper<ClientLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClientLog> buildQueryWrapper(ClientLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClientLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, ClientLog::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getClientCode()), ClientLog::getClientCode, bo.getClientCode());
        lqw.like(StringUtils.isNotBlank(bo.getClientName()), ClientLog::getClientName, bo.getClientName());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), ClientLog::getOperationType, bo.getOperationType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationDes()), ClientLog::getOperationDes, bo.getOperationDes());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ClientLog::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ClientLog::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, ClientLog::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), ClientLog::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, ClientLog::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增客户端日志
     */
    @Override
    public Boolean insertByBo(ClientLogBo bo) {
        ClientLog add = BeanUtil.toBean(bo, ClientLog.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户端日志
     */
    @Override
    public Boolean updateByBo(ClientLogBo bo) {
        ClientLog update = BeanUtil.toBean(bo, ClientLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ClientLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户端日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 上线
     */
    @Override
    public Boolean online(ClientLogBo bo){
        bo.setOperationType("保活操作");
        bo.setOperationType("客户端上线");
        bo.setCreateBy(Constant.ADMIN);
        bo.setCreateTime(new Date());
        bo.setUpdateBy(Constant.ADMIN);
        bo.setUpdateTime(new Date());
        return insertByBo(bo);
    }

    /**
     * 下线
     */
    @Override
    public Boolean offline(ClientLogBo bo){
        bo.setOperationType("保活操作");
        bo.setOperationType("客户端下线");
        bo.setCreateBy(Constant.ADMIN);
        bo.setCreateTime(new Date());
        bo.setUpdateBy(Constant.ADMIN);
        bo.setUpdateTime(new Date());
        return insertByBo(bo);
    }
}
